<template>
  <div class="my-goods-item">
    <div class="left">
      <div class="custom-control custom-checkbox">
        <input type="checkbox" class="custom-control-input" :id="goodsObj.id" v-model="goodsObj.goods_state" />
        <label class="custom-control-label" :for="goodsObj.id">
          <img
            :src="goodsObj.goods_img"
            alt=""
          />
        </label>
      </div>
    </div>
    <div class="right">
      <div class="top">{{goodsObj.goods_name}}</div>
      <div class="bottom">
        <span class="price">¥ {{goodsObj.goods_price}}</span>
        <div class="my-counter">
          <button type="button" class="btn btn-light" @click="reduce">-</button>
          <input type="number" class="form-control inp" v-model.number="goodsObj.goods_count" />
          <button type="button" class="btn btn-light" @click="add">+</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      
    };
  },
  props : {
    goodsObj : {
      type : Object,
      required : true
    }
  },
  methods : {
    reduce() {
      this.goodsObj.goods_count--
      if(this.goodsObj.goods_count < 1) {
        this.goodsObj.goods_count = 1
      }
    },
    add() {
      this.goodsObj.goods_count++
    }
  },
  watch : {
    goodsObj : {
      deep : true,
      handler() {
        if(this.goodsObj.goods_count < 0) {
          this.goodsObj.goods_count = 1
        } else if(!Number.isInteger(this.goodsObj.goods_count)) {
          this.goodsObj.goods_count = parseInt(this.goodsObj.goods_count)
        }
      }
    }
  }
};
</script>

<style scoped lang="less">
// 商品列表样式
.my-goods-item {
  display: flex;
  padding: 10px;
  border-bottom: 1px solid #ccc;
  .left {
    label {
      margin-left: 6px;
    }
    img {
      width: 120px;
      height: 120px;
      margin-right: 8px;
      border-radius: 10px;
    }
    .custom-control-label::before,
    .custom-control-label::after {
      top: 50px;
    }
  }
  .right {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    .top {
      font-size: 14px;
      font-weight: 700;
    }
    .bottom {
      display: flex;
      justify-content: space-between;
      padding: 5px 0;
      align-items: center;
      .price {
        color: red;
        font-weight: bold;
      }
      .my-counter {
        display: flex;
        .inp {
          width: 75px;
          outline: none !important;
          text-align: center;
          margin: 0 5px;
        }
        .btn {
          flex: 1;
          transform: scale(0.9);
        }
      }
    }
  }
}
</style>